package com.hospital.config.help;

import com.hospital.service.system.entity.SysLogInfo;
import com.hospital.service.system.entity.help.SysLogInfoHelp;
import com.hospital.service.system.service.SysLogInfoService;
import common.core.util.CommonUtil;
import common.core.util.SystemLog;
import enhance.plus.util.LogRecording;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/**
 * @author xhl
 */
@Aspect
@Slf4j
@Component
public class SystemLogging implements Serializable {

    @Autowired
    private SysLogInfoService sysLogInfoService;

    @Pointcut("@annotation(common.core.util.SystemLog)")
    public void logPointCut() {

    }


    @AfterThrowing(pointcut = "logPointCut()", throwing = "e")
    public void logStampException(JoinPoint joinPoint, Throwable e) throws Exception {
        log.info("操作出现异常时记录系统日志");
        List<String> list = getMethodDescription(joinPoint);
        SysLogInfo sysLogInfo = new SysLogInfo();
        sysLogInfo.setOperationName(list.get(0) + list.get(1) + ":" + list.get(2));
        sysLogInfo.setOperationResult(SysLogInfoHelp.operation_result_shibai);
        String message = e.getMessage();
        String s = null;
        if ( message != null && message.length() > 255) {
            s = message.substring(0, 50) + "...";
        }
        sysLogInfo.setRemarks("失败原因:" + s);
        sysLogInfoService.save(sysLogInfo);
    }

    @AfterReturning(pointcut = "logPointCut()")
    public void logStampRight(JoinPoint joinPoint) throws Exception {
        log.info("操作成功时记录系统日志");
        List<String> list = getMethodDescription(joinPoint);
        SysLogInfo sysLogInfo = new SysLogInfo();
        sysLogInfo.setOperationName(list.get(0) + list.get(1) + ":" + list.get(2));
        sysLogInfo.setOperationResult(SysLogInfoHelp.operation_result_chenggong);
        sysLogInfoService.save(sysLogInfo);
    }


    public List getMethodDescription(JoinPoint joinPoint) throws Exception{
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        SystemLog systemLog = method.getAnnotation(SystemLog.class);
        String description = systemLog.value();
        String logo = systemLog.logo();

        Object[] args = joinPoint.getArgs();
        Object o = null;
        for (Object arg : args) {
            if (arg instanceof LogRecording) {
                Class<?> aClass = arg.getClass();
                Field field = aClass.getDeclaredField(logo);
                field.setAccessible(true);
                o = field.get(arg);
            }
        }
        return Arrays.asList(description, logo, o);
    }
}
